package com.argrace.smart.activity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.argrace.smart.ArgraceApplication;
import com.argrace.smart.R;
import com.argrace.smart.base.ActivityManager;
import com.argrace.smart.base.BaseActivity;
import com.argrace.smart.base.ConstantConfigure;
import com.argrace.smart.model.LocalDb_QingJingTable;

import org.litepal.tablemanager.Connector;

/**
 * Created by argrace_dev_1 on 2018/2/11.
 */

public class CreateQingJingActivity extends BaseActivity {
    private EditText qingjingName;
    private TextView nextStep;
    private TextView qingjingSeat;
    private String qingjingSeat_roomname;
    private String uuid;
    private final int REQUEST_QINGJINGPOSITION = 401;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_createqingjing);
        init();
    }

    private void init() {
        uuid = getIntent().getStringExtra("uuid");
        setTitle(true, false, "创建场景");
        //进入设备修改保存状态界面
        setView();
    }

    private void setView() {
        qingjingName = (EditText) findViewById(R.id.activity_createqingjing_qingjingname);
        nextStep = (TextView) findViewById(R.id.activity_createqingjing_nextstep);
        qingjingSeat = (TextView) findViewById(R.id.activity_createqingjing_qingjingposition);
        nextStep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (qingjingName.getText() != null && !qingjingName.getText().toString().equals("")) {
                    LocalDb_QingJingTable qingjingBean = new LocalDb_QingJingTable();
                    //将网关设备的uuid设为deviceid作为区分
                    qingjingBean.setDeviceId(uuid);
                    qingjingBean.setName(qingjingName.getText().toString());
                    qingjingBean.setRoomName(qingjingSeat_roomname);
                    int result = qingjingBean.updateAll("deviceId = ? and name = ?", uuid, qingjingName.getText().toString());
                    if (result == 0) {
                        qingjingBean.save();
                    }
                    ArgraceApplication.is_RefreshQingJingTable = true;
                    finish();
                    try {
                        ActivityManager.getActivity(MineGwDeviceListActivity.class.getName()).finish();
                    } catch (Exception e) {

                    }

                }
            }
        });
        qingjingSeat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CreateQingJingActivity.this, MyRoomActivity.class);
                startActivityForResult(intent, REQUEST_QINGJINGPOSITION);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && resultCode == ConstantConfigure.RESULT_SUCCESS) {
            switch (requestCode) {
                case REQUEST_QINGJINGPOSITION:
                    String name = data.getStringExtra("name");
                    qingjingSeat_roomname = name;
                    qingjingSeat.setText(name);
                    break;
            }
        }


    }
}
